/****************************************************
 * 创建人:   robin
 * 创建时间: 2023/8/30.030 14:47
 * 项目名称: cpy
 * 文件名称: ResponseData.java
 * 文件描述: 
 *
 * All rights Reserved, Designed By 德品软件
 * @Copyright:2023-2023
 *
 ********************************************************/
package com.dp.vo.response;

import com.dp.enumeration.ReturnCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * 包名称：com.dp.vo.response
 * 类名称：ResponseData
 * 类描述：
 * 创建人： robin
 * 创建时间：2023/8/30.030 14:47
 */
public class ResponseData<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseData.class);
    
    public enum RspType {
        // 返回类型
        SUCCESS("SUCCESS", "成功"),               // 默认不打印日志
        WARN_YELLOW("WARN_YELLOW", "黄色警告"),     // 默认不打印日志
        WARN("WARN", "红色警告"),                   // 默认打印日志
        ERROR("ERROR", "错误");                   // 默认打印日志
        
        private String key;
        private String value;
        
        RspType(String rspKey, String value) {
            this.key = rspKey;
            this.value = value;
        }
        
        public String getKey() {
            return key;
        }
        
        public String getValue() {
            return value;
        }
    }
    
    private String code;
    private String msg;
    private RspType rspType;
    private transient T data;
    
    public String getCode() {
        return code;
    }
    
    public void setCode(String code) {
        this.code = code;
    }
    
    public String getMsg() {
        return msg;
    }
    
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    public RspType getRspType() {
        return rspType;
    }
    
    public void setRspType(RspType rspType) {
        this.rspType = rspType;
    }
    
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }
    
    private ResponseData() {
    
    }
    
    private ResponseData(RspType rspType, String code, String msg, T data) {
        this.rspType = rspType;
        this.code = code;
        this.msg = msg;
        this.data = data;
        
        switch (rspType) {
            case WARN:
                LOGGER.warn("{}:{}", code, msg);
                break;
            
            case ERROR:
                LOGGER.error("{}:{}", code, msg);
                break;
            
            default:
                break;
        }
    }
    
    /**
     *
     * @Title: handleSuccess
     * @Description: 返回成功消息
     * @param successMsg 消息
     * @param data 数据
     * @return
     * @return: ResponseData<T>
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:21:19
     */
    public static <T> ResponseData<T> handleSuccess(String successMsg, T data) {
        return new ResponseData<T>(RspType.SUCCESS, ReturnCodeEnum.SUCCESS.getCode(), successMsg, data);
    }
    
    /**
     *
     * @Title: handleSuccess
     * @Description: 返回成功消息
     * @param successMsg 消息
     * @return
     * @return: ResponseData<T>
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:24:21
     */
    public static <T> ResponseData<T> handleSuccess(String successMsg) {
        return new ResponseData<T>(RspType.SUCCESS, ReturnCodeEnum.SUCCESS.getCode(), successMsg, null);
    }
    
    /**
     *
     * @Title: handleSuccess
     * @Description: 返回成功消息
     * @param data 数据
     * @return
     * @return: ResponseData<T>
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:24:49
     */
    public static <T> ResponseData<T> handleSuccess(T data) {
        return new ResponseData<T>(RspType.SUCCESS, ReturnCodeEnum.SUCCESS.getCode(), ReturnCodeEnum.SUCCESS.getMsg(), data);
    }
    
    /**
     *
     * @Title: handleWarn
     * @Description: 返回警告消息
     * @param warnMsg 消息内容
     * @return
     * @return: ResponseData<T>
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:25:24
     */
    public static <T> ResponseData<T> handleWarn(String warnMsg) {
        return new ResponseData<T>(RspType.WARN, ReturnCodeEnum.WARN.getCode(), warnMsg, null);
    }
    
    /**handleWarn
     *
     * @Title: handleWarn
     * @Description: 返回警告消息
     * @param warnMsg 消息内容
     * @param data 数据
     * @return
     * @return: ResponseData
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:26:01
     */
    public static <T> ResponseData<T> handleWarn(String warnMsg, T data) {
        ResponseData<T> responseData = new ResponseData<>(RspType.WARN, ReturnCodeEnum.WARN.getCode(), warnMsg, data);
        return responseData;
    }
    
    /**handleWarn
     *
     * @Title: handleWarn
     * @Description: 返回警告消息
     * @param returnCodeEnum 结果码
     * @return
     * @return: ResponseData
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:26:01
     */
    public static <T> ResponseData<T> handleWarn(ReturnCodeEnum returnCodeEnum, Object... enumArgs) {
        ResponseData<T> responseData = new ResponseData<>(RspType.WARN, returnCodeEnum.getCode(), String.format(returnCodeEnum.getMsg(), enumArgs), null);
        return responseData;
    }
    
    /**handleWarn
     *
     * @Title: handleWarnYellow
     * @Description: 返回警告消息
     * @param returnCodeEnum 结果码
     * @return
     * @return: ResponseData
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:26:01
     */
    public static <T> ResponseData<T> handleWarnYellow(ReturnCodeEnum returnCodeEnum, Object... enumArgs) {
        ResponseData<T> responseData = new ResponseData<>(RspType.WARN_YELLOW, returnCodeEnum.getCode(), String.format(returnCodeEnum.getMsg(), enumArgs), null);
        return responseData;
    }
    
    
    /**
     *
     * @Title: handleError
     * @Description: 返回错误消息
     * @param code 错误代码
     * @param errorMsg 消息内容
     * @return
     * @return: ResponseData
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:26:55
     */
    public static <T> ResponseData<T> handleError(String code, String errorMsg) {
        ResponseData<T> responseData = new ResponseData<>(RspType.ERROR, code, errorMsg, null);
        return responseData;
    }
    
    /**
     *
     * @Title: handleError
     * @Description: 返回错误消息
     * @param code 错误代码
     * @param errorMsg 消息内容
     * @param data
     * @return
     * @return: ResponseData
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:27:28
     */
    public static <T> ResponseData<T> handleError(String code, String errorMsg, T data) {
        ResponseData<T> responseData = new ResponseData<>(RspType.ERROR, code, errorMsg, data);
        return responseData;
    }
    
    
    /**
     *
     * @Title: handleError
     * @Description: 返回错误消息
     * @param errorMsg
     * @return
     * @return: ResponseData
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:29:47
     */
    public static <T> ResponseData<T> handleError(String errorMsg) {
        ResponseData<T> responseData = new ResponseData<>(RspType.ERROR, ReturnCodeEnum.BIZ_ERROR.getCode(), errorMsg, null);
        return responseData;
    }
    
    
    /**
     *
     * @Title: handleError
     * @Description: 返回错误消息
     * @return
     * @return: ResponseData
     * @throws
     * @author: robin
     * @Date:  2021/7/14/下午2:30:12
     */
    public static <T> ResponseData<T> handleError() {
        ResponseData<T> responseData = new ResponseData<>(RspType.ERROR, ReturnCodeEnum.BIZ_ERROR.getCode(), ReturnCodeEnum.BIZ_ERROR.getMsg(), null);
        return responseData;
    }
    
    public static <T> ResponseData<T> handleError(ReturnCodeEnum returnCodeEnum, Object... enumArgs) {
        ResponseData<T> responseData = new ResponseData<>(RspType.ERROR, returnCodeEnum.getCode(), String.format(returnCodeEnum.getMsg(), enumArgs), null);
        return responseData;
    }
    
    @Override
    public String toString() {
        return "ResponseData [code=" + code + ", msg=" + msg + ", rspType=" + rspType + ", data=" + data + "]";
    }
    
}
